<template>
    <el-container class="home-container">
      <!-- 头部区域 -->
     <el-header>
       <!-- 左侧 -->
       <div class="header_left">
         <img src="../assets/logo.png" alt="">
         <span>商城后台管理系统</span>
       </div>
       <!-- 右侧 -->
       <el-button type="info" @click="logout">退出</el-button>
     </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
    <el-aside :width="collapse ? '65px' : '200px'">
      <!-- 侧边栏导航 -->
      <!-- unique-opened	是否只保持一个子菜单的展开 -->
      <!-- text-color	菜单的文字颜色 -->
      <!-- collapse	是否水平折叠收起菜单 -->
      <!-- collapse-transition	是否开启折叠动画 -->
      <!-- router	是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
      <!-- default-active	当前激活菜单的 index -->
      <el-menu
      class="el-menu-vertical-demo"
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF"
      unique-opened
      :collapse="collapse"
      :collapse-transition="false"
      router
      :default-active="activePath">
      <div class="menu-collapse" @click="toggleCollapse">|||</div>
      <!-- 一级菜单 -->
      <el-submenu :index="item.id+''" v-for='item in menulist' :key=item.id>
        <template slot="title">
          <!-- 图标 -->
          <i :class="iconsObj[item.id]"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <!-- subItem.path存储着路径 -->
          <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children"
          :key="subItem.id" @click="saveNavActivePath('/' + subItem.path)">
             <!-- 图标 -->
          <i class="el-icon-menu"></i>
          <!-- 文本 -->
          <span>{{subItem.authName}}</span>
          </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>

</template>

<script>
export default {
  created() {
    // 拉取菜单信息
    this.getMenuList()
    // 设置被选中的导航，让其高亮
   this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      // 菜单数组
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      collapse: false,
      activePath: ''

    }
  },
  methods: {
    logout() {
      // 退出功能，清空session
      window.sessionStorage.clear()
      // 退出返回登录界面
      this.$router.push('/login')
    },
    async getMenuList() {
      // .then用法  拿到menus列表
      // this.$http.get('/menus')
      // .then(function(res) {
      // console.log(res.data)
      // }
      // )
      // async await简化用法 拿到menus列表
      // 对象解构，把data赋值给res
      const { data: res } = await this.$http.get('menus')
        console.log(res)
       this.menulist = res.data
    },
    // 侧边栏折叠，点击后取反collapse
    toggleCollapse: function() {
      this.collapse = !this.collapse
    },
    // 保存选中导航的激活状态
    saveNavActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  padding: 5px;
  align-items: center;

}
.header_left {
  display: flex;
  align-items: center;
  img {
      width: 45px;
      height: 45px;
    }
    span {
      font-size: 20px;
      color: #fff;
      margin-left: 15px;
    }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0;
  }

}
.el-main {
  background-color: #eaedf1;
}
.menu-collapse {
  color: #fff;
  letter-spacing: 0.3em;
  text-align: center;
  cursor: pointer;
  background-color: #4a5064;
  line-height: 24px;
}
</style>
